import { Component, ViewChild} from '@angular/core';
import { IonicPage, NavController, NavParams, Slides, Content, AlertController} from 'ionic-angular';
import { LoadingController } from 'ionic-angular';
import { Https } from '../../app/http.service'
/**
 * Generated class for the TihuoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-tihuo',
	templateUrl: 'tihuo.html',
})
export class TihuoPage {
	@ViewChild(Slides) slides: Slides;
	@ViewChild(Content) content: Content;
	tihuoUrl = new Https().Url + "act=goods&op=tihuo";
	petArray = ['kittens','puppies']; 
	pet: string = "kittens";
	data: number;
	card: number;
	currentIndex: number;
	dataList = [];
	cardList = [];
	constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController, public loadingCtrl: LoadingController) {
	}

	ionViewDidEnter() {
		console.log('ionViewDidLoad TihuoPage');
		this.getList(0);
		//默认显示第一页
		
	} 
	getList(int){                                                                                                        
		let then = this;
		fetch(this.tihuoUrl, {
			method: "post",
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token"),
		}).then((response) => {
			response.text().then((data) => {
				if (data != '') {
					if(int == 0){

						then.dataList = null;
						then.cardList = null;
					}
					let json = JSON.parse(data).datas;
					if (json.code == '3') {
						then.alerts("错误，请重试");
						then.data = 0;
						then.card = 0;
					} else {
						json.chongzhi == 0 ? then.data = 0 : then.data = 1;
						json.gouwu == 0 ? then.card = 0 : then.card = 1;
						console.log(json);
						console.log(json.gouwu);
						then.dataList = json.chongzhi;
						then.cardList = json.gouwu;
						console.log(then.dataList);
					}
				} else {
					then.alerts("网络连接错误，请重试");
					then.data = 0;
					then.card = 0;
				}


			})
		}).catch((error) => {
			then.alerts(error);
		})
		this.currentIndex = 0
	}
	/* 
	 slide改变index触发
   */
	slideChanged(event) {
		let loader = this.loadingCtrl.create({
			content: "加载中",
			duration: 3000
		});
		this.getList(0)
		loader.present();
		//获取当前页面index
		let currentIndex = this.slides.getActiveIndex() > 1 ? 1 : this.slides.getActiveIndex();
		this.currentIndex = currentIndex;
		// console.log(currentIndex)
		//回到顶部
		this.content.scrollTo(0, 0, 0);
		//定位segment
		this.pet = this.petArray[currentIndex];
		//event.container : 高度
		//获取slide子元素
		let brother = event.container.children[0].children;
		// console.log(brother)
		for (let i = 0; i < brother.length; i++) {
			if (i != currentIndex) {
				if (brother[i].children[0].children[0].offsetHeight > brother[currentIndex].children[0].children[0].offsetHeight) {
					brother[i].children[0].children[0].style = "height:" + brother[currentIndex].children[0].children[0].offsetHeight + "px"
				}
			} else {
				brother[i].children[0].children[0].style = "height:unset"
			}

		}
		loader.dismiss();

	}
    /* 
      点击tab定位
    */
	swipeEvnet(event) {
		//跳转到指定页面
		//获取ID定位
		this.slides.slideTo(this.petArray.indexOf(event.target.id));
	}

	doInfinite(event){
		console.log("加载。");
		event.complete();
	}

	alerts(text) {
		let confirm = this.alertCtrl.create({
			title: '温馨提示',
			message: text,
			buttons: [
				{ text: '确定' },

			]
		});
		confirm.present();
	}
}
